const Koa = require('koa')
const Router = require('koa-router')
const compress = require('koa-compress')
const koaBody = require('koa-body')
const https = require('https')
const sslify  = require('koa-sslify').default
const fs = require('fs')
const cors = require('koa2-cors');

// 路由
const recommend = require('./router/recommend')
const category = require('./router/category')
const list = require('./router/list')
const text = require('./router/text')
const rank = require('./router/rank')
const bookinfo = require('./router/bookinfo')
const search = require('./router/search')
const searchBookinfo = require('./router/searchBookinfo')
const regist = require('./router/regist')
const login = require('./router/login')
const userList = require('./router/userList')
const upload = require('./router/upload')
const updateUser = require('./router/updateUser')
const bookList = require('./router/bookList')
const deleteUser = require('./router/deleteUser')
const updateBook = require('./router/updateBook')
const getBook81 = require('./router/getBook81')
const getBookCase = require('./router/getBookCase')
const updateBookCase = require('./router/updateBookCase')

const app = new Koa()
const router = new Router({ prefix: '/api' })

// http强转成https
// app.use(sslify())


// 文件压缩
const options = { threshold: 2048 };
app.use(compress(options));

app.use(koaBody({
  multipart:true,
  formidable: {
    maxFileSize: 200 * 1024 * 1024
  }
}));

// 跨域
app.use(cors());
// 静态文件
app.use(require('koa-static')(__dirname + '/public'))

// 路由
/* 用户 */
router.post('/regist',regist)  //注册
router.post('/login',login)  //登录
router.get('/userList',userList)  //用户列表
router.post('/updateUser', updateUser)  //修改用户
router.post('/deleteUser', deleteUser)  //删除用户
router.post('/upload',upload)  //上传文件
router.get('/getBookCase',getBookCase)
router.post('/updateBookCase',updateBookCase)
/* 小说 */
router.get('/bookList',bookList)  //小说列表
router.get('/rank', rank)   //首页排行
router.get('/recommend', recommend)  //首页推荐
router.post('/updateBook',updateBook)  //修改小说
router.get('/bookinfo', bookinfo)  //小说简介
router.get('/list', list)  //小说目录
router.get('/text', text)  //章节内容
router.get('/category', category)  //小说分类列表

/* 爬虫 */
router.get('/search', search)  //搜索小说列表
router.get('/searchBookinfo', searchBookinfo) //小说简介
router.get('/getBook81', getBook81)


app.use(router.routes())

app.listen(8888,()=>{
    console.log(`server running success at 8888`)
})

// const sslOptions = {
//     key:fs.readFileSync('./ssl/www.cczztt.club.key',"utf8"),
//     cert:fs.readFileSync('./ssl/www.cczztt.club.pem',"utf8")
// }
// https.createServer(sslOptions, app.callback()).listen(443, () => {
//     console.log(`server running success at 443`)
// });